/**
 * File Name   : LoginAction.java
 * Description : 
 */
package com.hh.admin.login.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.springframework.web.struts.ActionSupport;

import com.hh.admin.common.bean.LoginUserInfo;
import com.hh.admin.login.form.LoginForm;
import com.hh.admin.login.service.LoginService;
import com.hh.common.consts.AdminConst;

/**
 * 后台用户登录Action
 * @author 冯昌硕
 * @create 2012-12-6 下午8:40:28
 */
public class LoginAction extends ActionSupport {

    private static final Logger log = Logger.getLogger(LoginAction.class);
    
    private LoginService loginService;
    
    public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, 
            HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
        log.debug("[LoginAction] start.");
        
        LoginForm loginForm = (LoginForm) actionForm;
        
        // 获取前台输入的登录账号和密码 
        String account = loginForm.getAccount();
        String password = loginForm.getPassword();
        
        boolean result = loginService.checkUserLogin(account, password);
        log.debug("[LoginAction] result=["+ result +"]");
        if(result) {
            
            // 登录成功，设置用户Session
            LoginUserInfo loginUserInfo = new LoginUserInfo();
            loginUserInfo.setLoginName(account);
            servletRequest.getSession().setAttribute(AdminConst.SESSION_ATTR_USERINFO, loginUserInfo);
            
            return actionMapping.findForward("success");
        }
        
        // 登录错误信息
        ActionMessage errorMsg = new ActionMessage("login.error.notrightuser");
        ActionErrors errors = new ActionErrors();
        errors.add("login.error.notrightuser", errorMsg);
        addErrors(servletRequest, errors);
        
        return actionMapping.findForward("faild");
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }
}
